package com.dmap.admin.controller.backgroundv2;

import com.dmap.admin.bo.backgroundv2.AppBo;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.AppParam;
import com.dmap.admin.controller.backgroundv2.param.AppSelectParam;
import com.dmap.admin.po.App;
import com.dmap.admin.service.backgroundv2.AppService;
import com.dmap.admin.vo.backgroundv2.RecordNumAndAppVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 27/05/18
 * \* Description:
 * \
 */
@Api(value = "个人数据服务系统-App管理", description = "background/App管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v2/app/")
public class AppController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private AppService appService;

    @ResponseBody
    @ApiOperation(value = "新增 app",
            notes = "1. 必填： projectId, projectName; " +
                    "2. 返回id;")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseVo insert(@RequestBody AppParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getProjectId()) || StringUtils.isBlank(param.getProjectName())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        boolean paramIsExist = appService.validateProjectIdOrNameIsExist(param.getProjectId(), param.getProjectName());
        if (paramIsExist) {
            baseVo.setCode(AdminConstant.RESPONSE_APP_UNIQUE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_APP_UNIQUE_ERROR_MSG);
            return baseVo;
        }
        App insert = appService.insertApp(param);
        if (insert == null) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        baseVo.setData(insert.getId().toString());
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "修改 app",
            notes = "1. 必填： id, projectId, projectName; ")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseVo update(@RequestBody AppParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getId()) || !StringUtils.isNumeric(param.getId())
                || Long.valueOf(param.getId()).equals(0L)
                || StringUtils.isBlank(param.getProjectId()) || StringUtils.isBlank(param.getProjectName())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        boolean success;
        try {
            success = appService.updateApp(param);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            baseVo.setCode(AdminConstant.RESPONSE_APP_UNIQUE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_APP_UNIQUE_ERROR_MSG);
            return baseVo;
        }
        if (!success) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据id查询app", notes = "1. 必填： id;")
    @RequestMapping(value = "select/selectById", method = RequestMethod.GET)
    public BaseVo selectById(@RequestParam String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id) || !StringUtils.isNumeric(id) || Long.valueOf(id).equals(0L)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        App selectById = appService.selectById(Long.valueOf(id));
        List<AppBo> boList = new ArrayList<>();
        if (selectById != null) {
            boList.add(new AppBo(selectById));
        }
        RecordNumAndAppVo returnVo = new RecordNumAndAppVo(1, boList);
        baseVo.setData(returnVo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据参数查询app列表",
            notes = "1. 必填： pageIndex(>=1), pageSize(>=1); " +
                    "2. 返回app列表; ")
    @RequestMapping(value = "select/selectByExample", method = RequestMethod.POST)
    public BaseVo selectByExample(@RequestBody AppSelectParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || param.getPageIndex() == null || param.getPageSize() == null
                || param.getPageIndex() < 1 || param.getPageSize() < 1) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        int recordNum = appService.countByAppSelectParam(param);
        List<AppBo> boList = new ArrayList<>();
        if (recordNum > 0) {
            List<App> list = appService.selectByAppSelectParam(param);
            list.forEach(app -> boList.add(new AppBo(app)));
        }
        RecordNumAndAppVo returnVo = new RecordNumAndAppVo(recordNum, boList);
        baseVo.setData(returnVo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据companyId查询app列表", notes = "1. 必填： companyId; ")
    @RequestMapping(value = "select/selectByCompanyId", method = RequestMethod.GET)
    public BaseVo selectByCompanyId(@RequestParam String companyId) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(companyId) || !StringUtils.isNumeric(companyId)
                || Long.valueOf(companyId).equals(0L)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        List<AppBo> boList = new ArrayList<>();
        List<App> list = appService.selectByCompanyId(Long.valueOf(companyId));
        for (App app : list) {
            if (app != null) {
                boList.add(new AppBo(app));
            }
        }
        RecordNumAndAppVo returnVo = new RecordNumAndAppVo(boList.size(), boList);
        baseVo.setData(returnVo);
        return baseVo;
    }

}